<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Hello Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class SinhVienModelSinhVien extends JModel
{
	 var $_allData;
	 var $_allNganh;
	/**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	
	function _buildQuery() 
	{ 
		$query = ' SELECT * ' 
			   . ' FROM #__sinh_vien ' ; 
		return $query; 
	} 
	function getAllData() 
	{ 
		// Lets load the data if it doesn't already exist 
		if (empty( $this->_allData )) 
		{ 
			$query = $this->_buildQuery(); 
			$this->_allData = $this->_getList( $query ); 
		} 
		return $this->_allData; 
	}

	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$masv= JRequest::getVar('masv','','post');
		$tensv= JRequest::getVar('tensv','','post');
		$khoahoc= JRequest::getVar('khoahoc','','post');
		$lop= JRequest::getVar('lop','','post');
		$manganh= JRequest::getVar('manganh','','post');
		$quequan= JRequest::getVar('quequan','','post');
		$text = JRequest::getVar('text','','post');
		if($text=="New")
		{
			$query=" Insert into #__sinh_vien(masv,tensv,khoahoc,lop,manganh,quequan) 
			values('".$masv."','".$tensv."',".$khoahoc.",'".$lop."','".$manganh."','".$quequan."')";
		}
		else
		{
			$query=" Update #__sinh_vien set tensv ='".$tensv."' , khoahoc =".$khoahoc." , lop ='".$lop."' ,
			manganh ='".$manganh."',quequan ='".$quequan."' where masv='".$masv."'" ;
		}
		$db=& JFactory::getDBO();
		$db->setQuery($query);
		if(! $db->query())
		{
			return false;
		}
		return true;
	}

	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$db=& JFactory::getDBO();
		if (count( $cids )) {
			foreach($cids as $cid) {
				$sql="Delete from #__sinh_vien where masv='".$cid."'";
				$db->setQuery($sql);
				if (!$db->query()) {
					return false;
				}
			}
		}
		return true;
	}
}